<?php

namespace Admin\Controller;
/**用户控制器
 * Class UserController
 * @package Admin\Controller
 */
class UserController extends AdminController {

    /**用户等级
     * @author leiying
     */
    public function rankList(){
        if(IS_POST){

        }else{
            $lists = M('user_rank')->select();
            $this->assign('lists',$lists);
            $this->display();
        }
    }

    /**
     * 会员列表
     * @author leiying
     */
    public function userList(){
        $limit = LIMIT;
        $get = I('post.');

        $search_data = array();//筛选数据
        $where = array();//条件

        if(IS_POST){
            session('searchdata_users',$get);
        }else{
            if(I('get.p')){
                $this->assign('p',I('get.p'));
            }else{
                session('searchdata_users',null);
            }
            $get = $search_data = session('searchdata_users');
        }


        if(isset($get['user_phone']) && $get['user_phone'] != ''){
            $search_data['user_phone'] = $get['user_phone'];
            $where['a.user_phone'] = array('like','%'.$get['user_phone'].'%');
        }
         if(isset($get['user_rank']) && $get['user_rank'] != ''){
            $search_data['user_rank'] = $get['user_rank'];
            $where['a.user_rank'] = array('eq',trim($get['user_rank']));
        }
     
        $get['sort'] = isset($get['sort'])?$get['sort']:'desc';
        $search_data["sort"] = $get["sort"];
        

        $count = M('users')->where($where)->count();
        $Page = new  \Think\Page($count, $limit);// 实例化分页类 传入总记录数

        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $lists = M('users')->alias('a')
            ->where($where)
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->order("a.user_id ".$get['sort'])
            ->select();
      
        $this->assign('lists',$lists);
        $p = I('get.p',1);
        $this->assign('p',$p);
        $this->assign('searchs',$search_data);
        $this->display();
    }
//添加会员
      public function userAdd(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $mobile=isset($_POST['mobile'])?$_POST['mobile']:'';
            $info=M('users')->where(array('user_phone'=>$mobile))->find();
            if($info){
              $this->error('该手机号用户已存在！！！');
            }
            $data=array(
             user_name=>isset($_POST['username'])?$_POST['username']:'',
             user_phone=>isset($_POST['mobile'])?$_POST['mobile']:'',
             sex=>isset($_POST['sex'])?$_POST['sex']:'',
             password=>md5($_POST['pwd1']),
             user_rank=>isset($_POST['rank'])?$_POST['rank']:'',
             birthday=>$_POST['year'].'-'.$_POST['month'].'-'.$_POST['day'],
             parent_id=>isset($_POST['pid'])?$_POST['pid']:'',
             status=>isset($_POST['status'])?$_POST['status']:'',
             special_user=>isset($_POST['special'])?$_POST['special']:0,
             reg_time=>time(),
            );
           $result=M('users')->add($data);
           if($result){
             $this->success('添加成功');
           }else{
             $this->error('添加失败');
           }
          
        }else{
           $user_rank=M('user_rank')->select();
           $this->assign('rank',$user_rank);
           $this->display();
        }
       
    }
    //会员资金详情
    public function userAccount(){
        $uid=isset($_REQUEST['uid'])?$_REQUEST['uid']:'';
        $limit = LIMIT;
       
        $where = array();//条件
        $where['user_id'] = array('eq',$uid);
        $get['sort'] = isset($get['sort'])?$get['sort']:'desc';
        $count = M('user_account')->where($where)->count();

        $Page = new  \Think\Page($count, $limit);// 实例化分页类 传入总记录数

        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
       
        $lists = M('user_account')
            ->where($where)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order("log_id ".$get['sort'])
            ->select();
        $this->assign('lists',$lists);
        $p = I('get.p',1);
        $this->assign('p',$p);
        $this->assign('searchs',$search_data);
       $user=M('users')->where('user_id='.$uid)->find();
       $this->assign('user',$user);
       $this->display();
    }

    /**
     *修改会员
     */
    public function userEdit(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $data = I('post.');
            $password=$data['pass'];
            if(!empty($password)){
               $data['password']=md5($password);
               $data['key_salt']='';
            }
            $data['birthday']=$_POST['year'].'-'.$_POST['month'].'-'.$_POST['day'];
            $id = $data['id'];
            if(empty($id)){
                $this->error('请选择需要操作的数据');
            }
            unset($data['id']);
            $res = M('users')->where(array('user_id'=>$id))->save($data);
            if(is_numeric($res)){
                $this->success('编辑成功');
            }else{
                $this->error('编辑失败');
            }
        }else{
            $res = M('users')->where(array('user_id'=>I('id')))->find();
            $birthday=strtotime($res['birthday']);
            $res['year']=date('Y',$birthday);
            $res['month']=date('m',$birthday);
            $res['day']=date('d',$birthday);
            $this->assign('res',$res);
            $p = I('get.p',1);
            $this->assign('p',$p);
            $this->display(); 
        }
    }
//资金变动
    public function accountEdit(){
       if(I('post.btn')){$this->success('');}
       if(IS_POST){
            $data = I('post.');
            $id = $data['id'];
            if(empty($id)){
                $this->error('请选择需要操作的数据');
            }
            unset($data['id']);
            $money=isset($data['user_money'])?intval($data['user_money']):0;
            if($money!=0){
              $data_money['change_type']=$data['money_type'];
              if($data_money['change_type']==1){
                 $money=$money*(-1);
              }
              $data_money['user_money']=$money;
              $data_money['user_id']=$id;
              $data_money['change_time']=time();
              $data_money['change_desc']=$data['change_desc'];
              M('users')->where('user_id='.$data_money['user_id'])->setInc('user_money',$data_money['user_money']);
              $res=M('user_account')->add($data_money);
            }
            $jifen=isset($data['user_integral'])?intval($data['user_integral']):0;
            if($jifen!=0){
              $data_jf['change_type']=$data['jifen_type'];
              if($data_jf['change_type']==1){
                 $jifen=$jifen*(-1);
              }
              $data_jf['user_integral']=$jifen;
              $data_jf['user_id']=$id;
              $data_jf['change_time']=time();
              $data_jf['change_desc']=$data['change_desc'];
              M('users')->where('user_id='.$data_jf['user_id'])->setInc('user_integral',$data_jf['user_integral']);
              $res=M('user_account')->add($data_jf);
            }
            
            if(is_numeric($res)){
                $this->success('编辑成功');
            }else{
                $this->error('编辑失败');
            }
       }else{
         $res = M('users')->where(array('user_id'=>I('id')))->find();
         $this->assign('res',$res);
         $p = I('get.p',1);
         $this->assign('p',$p);
         $this->display();

       }
    }

    /**
     * 删除会员
     */
    public function updateStatus($method=null){
        if ( empty($_REQUEST['id']) ) {
            $this->error('请选择要操作的数据!');
        }
        $res = M("users")->where(array("user_id"=>I('id')))->delete();
        if($res){ 
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
    }

    //资金提现
    public function drawList(){
        $limit = LIMIT;
        $get = I('post.');

        $search_data = array();//筛选数据
        $where = array();//条件

        if(IS_POST){
            session('searchdata_users',$get);
        }else{
            if(I('get.p')){
                $this->assign('p',I('get.p'));
            }else{
                session('searchdata_users',null);
            }
            $get = $search_data = session('searchdata_users');
        }


        if(isset($get['user_name']) && $get['user_name'] != ''){
            $search_data['user_name'] = $get['user_name'];
            $where['a.user_name'] = array('like','%'.$get['user_name'].'%');
        }

        $get['sort'] = isset($get['sort'])?$get['sort']:'desc';
        $search_data["sort"] = $get["sort"];
        

        $count = M('user_drawings')->where($where)->count();
        $Page = new  \Think\Page($count, $limit);// 实例化分页类 传入总记录数

        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $lists = M('user_drawings')->alias('b')
            ->join('km_users a on b.user_id = a.user_id','left')
            ->field('a.user_name,b.*')
            ->where($where)
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->order("b.id ".$get['sort'])
            ->select();
        $this->assign('lists',$lists);
        $p = I('get.p',1);
        $this->assign('p',$p);
        $this->assign('searchs',$search_data);
        $this->display();
    }
//提现操作
    public function drawEdit(){
      if(I('post.btn')){$this->success('');}
      if(IS_POST){
          $data = I('post.');
          $id = $data['id'];
          if(empty($id)){
                $this->error('请选择需要操作的数据');
          }
          $data['admin_user']=NICKNAME;
          $data['paid_time']=time();
          $res = M('user_drawings')->where(array('id'=>$id))->save($data);

          $log=array(
              'user_id'=>$data['uid'],
              'change_desc'=>$data['admin_note'],
              'change_time'=>$data['paid_time'],
              'user_money'=>$data['money'],
              'change_type'=>3
              );
          M('user_account')->add($log);
      
         
          if(is_numeric($res)){
             $this->success('编辑成功');
          }else{
             $this->error('编辑失败');
          }
         exit;
      }else{
         //$res=M('user_drawings')->where(array('id'=>I('id')))->find();
         $res= M('user_drawings')->alias('b')
              ->join('km_users a on b.user_id = a.user_id','left')
              ->field('a.user_name,b.*')
              ->where(array('b.id'=>I('id')))
              ->find();
         // echo M()->getLastSql();
         // exit;
         $this->assign('res',$res);
         $p = I('get.p',1);
         $this->assign('p',$p);
         $this->display();

      }
     
    }

    public function rankEdit(){
      if(I('post.btn')){$this->success('');}
      if(IS_POST){
          $data = I('post.');
          $id = $data['id'];
          if(empty($id)){
                $this->error('请选择需要操作的数据');
          }
          $result=M('user_rank')->where('rank_id='.$id)->save($data);
          if(is_numeric($result)){
             $this->success('编辑成功');
          }else{
             $this->error('编辑失败');
          }
      }else{
        $res=M('user_rank')->where('rank_id='.I('id'))->find();
        $this->assign('res',$res);
        $this->display();
      }
    }
    //实名认证
    public function userAuth(){

        $limit = LIMIT;
        $get = I('post.');

        $search_data = array();//筛选数据
        $where = array();//条件

        if(IS_POST){
            session('searchdata_users',$get);
        }else{
            if(I('get.p')){
                $this->assign('p',I('get.p'));
            }else{
                session('searchdata_users',null);
            }
            $get = $search_data = session('searchdata_users');
        }


        if(isset($get['user_phone']) && $get['user_phone'] != ''){
            $search_data['user_phone'] = $get['user_phone'];
            $where['a.user_phone'] = array('like','%'.$get['user_phone'].'%');
        }
         if(isset($get['auth_status']) && $get['auth_status'] != ''){
            $search_data['auth_status'] = $get['auth_status'];
            $where['a.auth_status'] = array('eq',trim($get['auth_status']));
        }
        $where['a.auth_type'] =1;
        $get['sort'] = isset($get['sort'])?$get['sort']:'desc';
        $search_data["sort"] = $get["sort"];
        
        $count = M('user_auth')->where($where)->count();
        $Page = new  \Think\Page($count, $limit);// 实例化分页类 传入总记录数

        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $lists = M('user_auth')->alias('a')
            ->where($where)
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->order("a.add_time ".$get['sort'])
            ->select();
        $this->assign('lists',$lists);
        $p = I('get.p',1);
        $this->assign('p',$p);
        $this->assign('searchs',$search_data);
      $this->display();
    }

     public function authCheck(){
      if(I('post.btn')){$this->success('');}
      $res=M('user_auth')->where('auth_id='.I('id'))->find();
      $this->assign('res',$res);
      $this->display();
    }

    //病历
    public function medicalRecord(){
      $this->display();
    }
}
